<template>
  <div>
    <FormPanel ref="formPanel" :list="formList" :formType="type" @formChange="formChangeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'center'
      }"
    >
      <multiuse-button type="primary" :click="dataHandle">保 存</multiuse-button>

      <el-button @click="closeDrawer">关 闭</el-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { saveData } from '@/api/dealerFinance/InvoiceManage/AddresseeInfoMaintian';
import { phoneAndSolidify, telePhone, phone } from '@/utils/validate.js';

export default {
  name: 'SonModel',
  mixins: [dictionary],

  props: ['type', 'rowData'],
  data() {
    this.BaseTable = null;

    return {
      formList: this.createdFrom()
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      if (this.type == 'add') return;
      this.formList.map(x => {
        x.initialValue = this.rowData[x.fieldName];
      });
      this.formList.find(x => x.fieldName == 'cityId').itemList = this.createcityList(this.rowData.provinceId);
      this.formList.find(x => x.fieldName == 'districtId').itemList = this.district(this.rowData.cityId);
    },
    formChangeHandle(val) {
      val.provinceName = this.createRegionsList().find(x => x.value == val.provinceId).text;
      val.cityName = this.createcityList(val.provinceId).find(x => x.value == val.cityId).text;
      val.districtName = this.district(val.cityId).find(x => x.value == val.districtId).text;
      this.saveData = val;
    },
    async dataHandle() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (this.saveData) {
        // 编辑传ID
        if (this.type == 'edit') {
          this.saveData.id = this.rowData.id ? this.rowData.id : '';
        }
        let res = await saveData(this.saveData);

        if (res.resultCode == 200) {
          this.$notify.success({ title: '提示信息', message: '操作成功' });
          this.$emit('close', false, 'refresh');
        }
      }
    },
    closeDrawer() {
      this.$emit('close', false);
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '姓名',
          fieldName: 'recipientName',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '邮编',
          fieldName: 'recipientPostCode',
          maxlength: 6,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '电话',
          fieldName: 'recipientPhone',
          maxlength: 12,
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '移动电话',
          fieldName: 'recipientMobile',
          maxlength: 11,
          placeholder: '请输入',
          rules: [
            { required: true, message: '请输入', trigger: 'blur' },
            {
              validator: phone,
              trigger: 'blur'
            }
          ]
        },
        {
          type: 'SELECT',
          label: '省份',
          fieldName: 'provinceId',
          placeholder: '请选择',
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          itemList: this.createRegionsList(),
          change: p => {
            if (p) {
              this.formList.map(x => {
                if (x.fieldName == 'cityId') {
                  x.initialValue = '';
                  x.itemList = this.createcityList(p);
                }
              });
            } else {
              this.formList.map(x => {
                if (x.fieldName == 'cityId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
                if (x.fieldName == 'districtId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
            }
          }
        },
        {
          type: 'SELECT',
          label: '所在市',
          fieldName: 'cityId',
          placeholder: '请选择',
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          itemList: [],
          change: p => {
            if (p) {
              this.formList.map(x => {
                if (x.fieldName == 'districtId') {
                  x.initialValue = '';
                  x.itemList = this.district(p);
                }
              });
            } else {
              this.formList.map(x => {
                if (x.fieldName == 'districtId') {
                  x.itemList = [];
                  x.initialValue = '';
                }
              });
            }
          }
        },
        {
          type: 'SELECT',
          label: '县区',
          fieldName: 'districtId',
          placeholder: '请选择',
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          itemList: []
        },
        {
          type: 'INPUT',
          label: '详细地址',
          fieldName: 'address',
          selfCols: 2,
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'SELECT',
          label: '业务类型',
          fieldName: 'businessType',
          placeholder: '请选择',
          itemList: this.createDictList('6089')
        }
      ];
    }
  }
};
</script>
